<?php
	session_start();
	include_once("global.php");
	$uid = $_SESSION['name'];
	$smarty->assign("uid",$uid);
	$logindate = date("Y-m-d");
	$url = $_SERVER['REQUEST_URI'];
	$url_arr = parse_url($url);
	parse_str($url_arr['query'],$url_arr);
	$url_id = $url_arr[id];
	$smarty->assign("urlid",$url_id);

	if(isset($_POST['register'])){
		if(!empty($_POST['login_pwd']) && !empty($_POST['login_name']) && !empty($_POST['email'])){
			$login_name = $_POST['login_name'];
			$sql_check_loginname = "select U_UID from userinfo where U_Loginid = '$login_name'";
			$query_check_loginname = $mysqli->query($sql_check_loginname);
			$result_check_loginname = $query_check_loginname->fetch_assoc();
			if(!$result_check_loginname){
				$login_email = $_POST['email'];
				$sql_check_loginemail = "select U_UID from userinfo where U_email = '$login_email'";
				$query_check_loginemail = $mysqli->query($sql_check_loginemail);
				$result_check_loginemail = $query_check_loginemail->fetch_assoc();
				if(!$result_check_loginemail){
					if($_POST['login_pwd'] == $_POST['repeat_pwd']){
						$login_pwd = md5($_POST['login_pwd'].$str);
						$sql_insert_user = "insert into userinfo (U_UID,U_Loginid,U_Password,U_email,U_Roleid,U_Integral,U_Consume,U_Logindate,U_Downloadcount) values (NULL,'$login_name','$login_pwd','$login_email','0','0','0','$logindate','0')";
						$query_insert_user = $mysqli->query($sql_insert_user);
						if($query_insert_user){
							$sql_select_uid = "select U_UID from userinfo where U_Loginid = '$login_name' and U_email = '$login_email'";
							$query_select_uid = $mysqli->query($sql_select_uid);
							$result_select_uid = $query_select_uid->fetch_assoc();
							if($result_select_uid['U_UID']){
								$_SESSION['name'] = $result_select_uid['U_UID'];
								echo "<script>alert('注册成功".$_SESSION['name']."')</script>";
								echo "<meta http-equiv=\"refresh\" content=\"1; url='index.html' />";
							}else{
								echo "<script>alert('注册失败,请重新注册')</script>";
								echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=1' />";
							}
						}else{
							echo $mysqli->error;
						}
					}else{
						echo "<script>alert('注册失败,两次密码不相同')</script>";
						echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=1' />";
					}
				}else{
					echo "<script>alert('注册失败,邮箱已经存在')</script>";
					echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=1' />";
				}				
			}else{
				echo "<script>alert('注册失败,登录名已经存在')</script>";
				echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=1' />";
			}			
		}else{
			echo "<script>alert('注册按照要求填写信息')</script>";
			echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=1' />";
		}
	}

	if(isset($_POST['login'])){
		$login_pwd = md5($_POST['login_pwd'].$str);
		$login_name = $_POST['login_name'];
		$sql_login_name = "select U_UID,U_Password,U_Logindate from userinfo where U_Loginid = '$login_name'";
		$query_login_name = $mysqli->query($sql_login_name);
		$result_login_name = $query_login_name->fetch_assoc();
		if($result_login_name){
			$check_pwd = $result_login_name['U_Password'];
			if($check_pwd == $login_pwd){
				$loginuid = $result_login_name['U_UID'];
				$_SESSION['name'] = $loginuid; 
				if($date != $result_login_name['U_Logindate']){					
					$sql_update_logindate = "update userinfo set U_Logindate = '$logindate;',U_Downloadcount = '0' where U_UID = '$loginuid'";
					$query_update_logindate = $mysqli->query($sql_update_logindate);
					if($query_update_logindate){
						echo "<script>alert('登录成功')</script>";
						echo "<meta http-equiv=\"refresh\" content=\"1; url='index.html' />";
					}
				}else{
					echo "<script>alert('登录成功')</script>";
					echo "<meta http-equiv=\"refresh\" content=\"1; url='index.html' />";
				}
				
			}else{
				echo "<script>alert('用户名密码错误')</script>";
				echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=2' />";
			}
		}else{
			echo "<script>alert('用户名密码错误')</script>";
			echo "<meta http-equiv=\"refresh\" content=\"1; url='login.php?id=2' />";
		}
	}
	
	//头部
	$sql_select_search = "select K_Name,K_KID from keywordinfo where K_search > '1' limit 0,4";
	$query_select_search = $mysqli->query($sql_select_search);
	while($result_select_search = $query_select_search->fetch_assoc()){
		$assign_search[] = array(	"num"	=>	$result_select_search['K_KID'],
									"name"	=>	$result_select_search['K_Name']);
	}
	$smarty->assign("assign_search",$assign_search);/*热门关键词*/
	
	$sql_select_skeyword = "select K_Name,K_KID from keywordinfo where K_search = '1'";
	$query_select_skeyword = $mysqli->query($sql_select_skeyword);
	while($result_select_skeyword = $query_select_skeyword->fetch_assoc()){
		$assign_skeyword[] = array(	"num"	=>	$result_select_skeyword['K_KID'],
									"name"	=>	$result_select_skeyword['K_Name']);
	}
	$smarty->assign("assign_skeyword",$assign_skeyword);/*搜索框内关键词*/
	
	$sql_select_articlecategory = "select * from categoryinfo limit 0,2";
	$query_select_articlecategory = $mysqli->query($sql_select_articlecategory);
	while($result_select_articlecategory = $query_select_articlecategory->fetch_assoc()){
		$assign_artcategory[] = array (	"id"	=>	$result_select_articlecategory['C_CID'],
										"name"	=>	$result_select_articlecategory['C_Categoryname']);
	}
	$smarty->assign("assign_artcategory",$assign_artcategory);/*导航条*/
	
	$sql_select_countapp = "select * from appinfo";
	$query_select_countapp = $mysqli->query($sql_select_countapp);
	$result_select_countapp = $query_select_countapp->num_rows;
	$assign_countapp = $result_select_countapp;
	$smarty->assign("assign_countapp",$assign_countapp);/*站内APP统计*/
	
	$random = rand(50,$assign_countapp);
	$assign_radnom = $random;
	$smarty->assign("assign_radnom",$assign_radnom);	
	
	//通栏推广位
	$sql_select_banner = "select * from pushinfo where P_Pushid >30";
	$query_select_banner = $mysqli->query($sql_select_banner);
	while($result_select_banner = $query_select_banner->fetch_assoc()){
		$banner_appid = $result_select_banner['P_Pushappid'];
		$banner_apppid = $result_select_banner['P_Pushpid'];
		
		$sql_select_bannerpic = "select * from pictureinfo where P_PID = '$banner_apppid'";
		$query_select_bannerpic = $mysqli->query($sql_select_bannerpic);
		$result_select_bannerpic = $query_select_bannerpic->fetch_assoc();
		
		$sql_select_bannerapp = "select * from appinfo where A_Apptrackid = '$banner_appid'";
		$query_select_bannerapp = $mysqli->query($sql_select_bannerapp);
		$result_select_bannerapp = $query_select_bannerapp->fetch_assoc();
		
		$assign_bannerapp[] = array(	"id"	=>	$banner_appid,
										"name"	=>	$result_select_bannerapp['A_Apptrackname'],
										"url"	=>	"images/".$result_select_bannerpic['P_Name'].".png",);
	}
	$smarty->assign("assign_bannerapp",$assign_bannerapp);/*顶部通栏，首页通栏1，首页通栏2*/	
//	//APP排行榜
	$sql_app_rank = "select * from appinfo where A_Appprice = '免费' order by A_Appdate desc limit 0,6";
	$query_app_rank = $mysqli->query($sql_app_rank);
	while($result_app_rank = $query_app_rank->fetch_assoc()){
		$assign_apprank[] = array(	"id"	=>	$result_app_rank['A_Apptrackid'],
									"name"	=>	$result_app_rank['A_Apptrackname'],
									"icon"	=>	$result_app_rank['A_Appicon'],
								);
	}
	$smarty->assign("assign_apprank",$assign_apprank);/*免费排名*/
	
	$sql_select_newstop5 = "select A_AID,A_Title from articleinfo where A_CID='1' order by A_date desc limit 0,5";
	$query_select_newstop5 = $mysqli->query($sql_select_newstop5);
	while($result_select_newstop5 = $query_select_newstop5->fetch_assoc()){
		$assign_newstop5[] = array(	"id"		=>	$result_select_newstop5['A_AID'],
									"title"		=>	$result_select_newstop5['A_Title']
									);
	}
	$smarty->assign("assign_newstop5",$assign_newstop5);/*新闻前五篇*/
	$sql_select_newstop10 = "select A_AID,A_Title from articleinfo where A_CID='1' order by A_date desc limit 5,5";
	$query_select_newstop10 = $mysqli->query($sql_select_newstop10);
	while($result_select_newstop10 = $query_select_newstop10->fetch_assoc()){
		$assign_newstop10[] = array(	"id"		=>	$result_select_newstop10['A_AID'],
										"title"		=>	$result_select_newstop10['A_Title']
								);
	}
	$smarty->assign("assign_newstop10",$assign_newstop10);/*新闻后五篇*/
	
	$sql_site_info = "select * from siteinfo";
	$query_site_info = $mysqli->query($sql_site_info);
	while($result_site_info = $query_site_info->fetch_assoc()){
		$assign_info[] = array( "id"	=>	$result_site_info['S_SID'],
								"info"	=>	$result_site_info['S_Name'],
								);
	}
	$smarty->assign("assign_info",$assign_info);

	$smarty->display("login.html");
	$smarty->display("footer.html");
?>